package com.android.utility;

import java.io.File;

import android.database.Cursor;
import android.os.Environment;

public class Utils {

	public static String getColumnValue(Cursor cursor, String colName) {
		int index = cursor.getColumnIndex(colName);
		return cursor.getString(index);
	}

	public static String getCursorColumnNames(Cursor cursor) {
		int count = cursor.getColumnCount();
		StringBuffer cNameBuffer = new StringBuffer();
		for (int i = 0; i < count; i++) {
			String cName = cursor.getColumnName(i);
			cNameBuffer.append(cName).append(";");
		}
		return cNameBuffer.toString();
	}

	/**
	 * Method to check whether external media available and wirtable.
	 * 
	 * @return
	 */
	public static boolean checkExternalMedia() {
		boolean externalStorageAvailable = false;
		boolean externalStorageWriteable = false;
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			// we can read and write the media
			externalStorageAvailable = externalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			externalStorageAvailable = true;
			externalStorageWriteable = false;
		} else {
			// we can't read or write
			externalStorageAvailable = externalStorageWriteable = false;
		}

		return (externalStorageAvailable & externalStorageWriteable) ? true
				: false;
	}

	/**
	 * Find the root of the external storage and output external storage info
	 * 
	 * @return
	 */
	public static String getExternalStorageRoot() {
		return Environment.getExternalStorageDirectory().getAbsolutePath();
	}

	public static String getMyExternalBackup() {
		return getExternalStorageRoot() + File.separator + "my_backup"
				+ File.separator;
	}
}
